<template>
  <!-- 所有成员列表 -->
  <div class="material-auth">
    <div class="list" v-if="list.length">
      <div class="item" v-for="(item, index) in list" :key="index">
        <img src="@/assets/image/platform-avatar.png" alt="" class="avatar"/>
        <div class="item-center">
          <span class="name">{{ item.username}}</span>
          <span class="auth">{{ item.type }}</span>
        </div>
        <span class="change-btn" @click="changeAuth(item)">修改授权</span>
      </div>
    </div>

    <div class="no-data" v-else>
       <span>暂无商户</span>
    </div>
  </div>
</template>

<script>
import { listMembers } from '@/api/account'
import { getOssDomain } from '@/api/oss'

export default {
  data () {
    return {
      current_page: 1,
      page_size: 20,
      order_by: 'SUM_VIEW',
      list: [],
      total: 0,
      authDict: {
        'ALL_AUTHORIZE': '全部授权',
        'PART_AUTHORIZE': '部分授权',
        'NOT_AUTHORIZE': '不授权'
      },
      oss_domain: '',
    }
  },
  async mounted () {
    if (!sessionStorage.getItem('oss_domain')) {
      const res = await getOssDomain()
      sessionStorage.setItem('oss_domain', res.domain)
      this.oss_domain = res.domain
    } else {
      this.oss_domain = sessionStorage.getItem('oss_domain')
    }
    this.getAccountList()
    document.title = '素材包授权'
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll,true) // 监听页面滚动*/
  },
  methods: {
    handleScroll (e) {
      if (this.loading) return
      const diff = 100
      //变量scrollTop是滚动条滚动时，距离顶部的距离
      let scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
      //变量windowHeight是可视区的高度
      let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
      //变量scrollHeight是滚动条的总高度
      let scrollHeight = document.documentElement.scrollHeight||document.body.scrollHeight;
      if (scrollHeight - scrollTop- windowHeight < diff) {
        console.log('page member list - 到底部啦')
        if (this.list.length < this.total) {
          this.current_page += 1
          this.getAccountList()
        }
      }
    },
    getAccountList () {
      this.loading = true
      const { order_by, page_size, current_page} = this
      listMembers({
        merchant_id: this.$route.query.merchant_id,
        current_page, order_by, page_size,
        status: 'AUDIT_PASS'
      }).then(res => {
        console.log('查看素有成员：', res)
        res.list.forEach(item => {
          item.type = this.authDict[item.authorize_type_enum]
          if (item.head_image) item.head_image = `${this.oss_domain}/${item.head_image}`
        })
        this.list = res.list
        this.total = res.total
        this.loading = false
      })
    },
    changeAuth (item) {
      this.$router.push({
        path: '/changeMaterialAuth',
        query: {
          type_key: item.authorize_type_enum,
          account_id: item.id
        }
      })
    }
  },
  beforeDestroy () {
    window.removeEventListener('scroll', this.handleScroll, true) // 监听页面滚动*/
  }
}
</script>

<style scoped>
.no-data {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
}
.material-auth {
  padding-top: .6vw;
  min-height: 100vh;
  display: flex;
  box-sizing: border-box;
  flex-direction: column;
  justify-content: center;
}
.list {
  padding-top: 4.5vw;
  flex: 1;
}
.item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 89.6vw;
  height: 19.2vw;
  box-sizing: border-box;
  margin: 0 auto 3.2vw;
  background: #ffffff;
  border-radius: 2.13vw;
  padding: 0 2.67vw 0 3.2vw;
}
.item-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.avatar {
  width: 12.27vw;
  height: 12.27vw;
  border-radius: 50%;
  margin-right: 2.13vw;
}
.name {
  margin-bottom: 0.4vw;
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: bold;
  line-height: 5.87vw;
  color: #3D3D3D;
}
.auth {
  font-family: Source Sans Pro;
  font-size: 3.2vw;
  font-weight: 600;
  line-height: 5.87vw;
  color: #00A6A7;
}
.change-btn {
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: 600;
  color: #0A67FF;
}
.gray {
  color: #999999;
}
</style>